package com.tjsinfo.tjsoa.addresslist.bo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.Cookie;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.common.page.UserInfoPage;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListGroup;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListUser;

public class AddressListUserPageBo implements IAddressListUserPageBo {
	private IQbcDao qbcDao;
	private IAddressListGroupBo groupBo;
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getQbcDao()
	 */
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	private static int pagecount = 0;//主页初始化时，当前组内联系人信息的页数


	public static int getPagecount() {
		return pagecount;
	}
	
	private static String group = "";//主页初始化时，组列表中第一个组ID
	public static String getGroup() {
		return group;
	}
	
	private static int page = 0;//联系人信息列表跳转到最后一页时，最后一页是第几页


	public static int getPage() {
		return page;
	}
	public static int pagenum = 17;//如果外部方法没有传参，联系人列表多少条一页，则默认此数量
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateGotoPage(java.lang.String, int)
	 */
	public List<AddressListUser> getGotoPage(String group_id,int page,String type,String pagenum1) {
		SimpleExpression a = Restrictions.eq("id", group_id);
		SimpleExpression c = Restrictions.eq("moduleType", Short.valueOf(type));
		LogicalExpression d = Restrictions.and(a, c);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListUser.class);
		
		String tongxunluFabuGroupId = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("tongxunluFabuGroupId");
		if (tongxunluFabuGroupId!=null) {
			if (tongxunluFabuGroupId.equals(group_id)) {
				criteria.add(Restrictions.eq("remarks", "是"));
			}
		}
		
		DetachedCriteria criteria2 = criteria.createCriteria("addressListGroup");
		criteria2.add(d);
		if("1".equals(type)){
			criteria.addOrder(Order.asc("createTime"));
		}else {
			criteria.addOrder(Order.asc("userSort"));
		}
		
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		UserInfoPage userPage = new UserInfoPage(list);
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		pagecount = userPage.getPageCount();
		if (page > pagecount) {
			userPage.gotoPage(pagecount);
		}else {
			userPage.gotoPage(page);
		}
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateLastPage(java.lang.String)
	 */
	public List<AddressListUser> getLastPage(String group_id,String type,String pagenum1) {
		SimpleExpression a = Restrictions.eq("id", group_id);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListUser.class);
		
		String tongxunluFabuGroupId = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("tongxunluFabuGroupId");
		if (tongxunluFabuGroupId!=null) {
			if (tongxunluFabuGroupId.equals(group_id)) {
				criteria.add(Restrictions.eq("remarks", "是"));
			}
		}
		
		DetachedCriteria criteria2 = criteria.createCriteria("addressListGroup");
		criteria2.add(a);
		if("1".equals(type)){
			criteria.addOrder(Order.asc("createTime"));
		}else {
			criteria.addOrder(Order.asc("userSort"));
		}
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		page = userPage.getPageCount();
		userPage.gotoPage(userPage.getPageCount());
		pagecount = userPage.getPageCount();
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateFirstPage(int)
	 */
	public List<AddressListUser> getFirstPage(int user_id,String type,String pagenum1) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListGroup.class);
		criteria.add(Restrictions.eq("moduleType", Short.parseShort(type)));
		criteria.addOrder(Order.asc("createTime"));
		
		if("1".equals(type)){
			DetachedCriteria criteria1= criteria.createCriteria("tjsUser");
			SimpleExpression d = Restrictions.eq("id", user_id);
			criteria1.add(d);
		}
		
		ArrayList<AddressListGroup> grouplist = (ArrayList)qbcDao.getCriteria(criteria);
		if (grouplist.size()==0) {
			groupBo.insertGroup("我的联系人", user_id, "1", 1);
			criteria = DetachedCriteria.forClass(AddressListGroup.class);
			criteria.add(Restrictions.eq("moduleType", Short.parseShort(type)));
			criteria.addOrder(Order.asc("createTime"));
			
			if("1".equals(type)){
				DetachedCriteria criteria1= criteria.createCriteria("tjsUser");
				SimpleExpression d = Restrictions.eq("id", user_id);
				criteria1.add(d);
			}
			
			grouplist = (ArrayList)qbcDao.getCriteria(criteria);
		}
		ArrayList list1 = new ArrayList();
		if(grouplist.size()>0){
			group = grouplist.iterator().next().getId();
			
			String tongxunluFabuGroupId = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("tongxunluFabuGroupId");
			System.out.println("tongxunluFabuGroupId="+tongxunluFabuGroupId);
			if (tongxunluFabuGroupId!=null) {
				if (tongxunluFabuGroupId.equals(group)) {
					criteria.add(Restrictions.eq("remarks", "是"));
				}
			}
			
			SimpleExpression f = Restrictions.eq("addressListGroup", grouplist.iterator().next());
			
			DetachedCriteria criteria2 = DetachedCriteria.forClass(AddressListUser.class);
			criteria2.add(f);
			if("1".equals(type)){
				criteria.addOrder(Order.asc("createTime"));
			}else {
				criteria.addOrder(Order.asc("userSort"));
			}
			ArrayList list = new ArrayList();
			try {
				list = (ArrayList)qbcDao.getCriteria(criteria2);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			UserInfoPage userPage = new UserInfoPage(list);
			
			if (pagenum1 != null) {
				userPage.setPageSize(Integer.valueOf(pagenum1));
			}else {
				userPage.setPageSize(pagenum);
			}
			userPage.gotoPage(1);
			pagecount = userPage.getPageCount();
			list1 = new ArrayList(userPage.getPageData());
		}
		
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPageCount(java.lang.String)
	 */
	public int getPageCount(String group_id,String pagenum1) {
		SimpleExpression a = Restrictions.eq("id", group_id);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListUser.class);
		
		String tongxunluFabuGroupId = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("tongxunluFabuGroupId");
		if (tongxunluFabuGroupId.equals(group_id)) {
			criteria.add(Restrictions.eq("remarks", "是"));
		}
		
		DetachedCriteria criteria2 = criteria.createCriteria("addressListGroup");
		criteria2.add(a);
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		return userPage.getPageCount();
	}
	public void setGroupBo(IAddressListGroupBo groupBo) {
		this.groupBo = groupBo;
	}
}
